const Common_User_Model = require("../models/CommonUserModel")

const Common_User_Service = {
    add_user: async (data, cryptogram) => {
        return Common_User_Model.create({
            ...data,
            cryptogram: cryptogram
        })
    },
    getList: async (id) => {
        return {
            data: await Common_User_Model.find().select('-password -__v').limit(5).skip(id),
            count: await Common_User_Model.countDocuments()
        }
    },
    put_user: async (data) => {
        return await Common_User_Model.updateOne({
            _id: data._id
        }, {
            username: data.username,
            cryptogram: data.cryptogram,
            password: data.password,
            role: data.role
        })
    },
    delete_user: async (_id) => {
        return await Common_User_Model.deleteOne({
            _id: _id
        })
    },
    login: async (data) => {
        return await Common_User_Model.findOne({
            ...data
        }).select('-__v -cryptogram -password')
    },
    user_upload: async (id, url) => {
        return await Common_User_Model.updateOne({
            _id: id
        }, {
            avatar: url
        })
    }
}



module.exports = Common_User_Service